package com.hw.backapi.controller;

import com.hw.backapi.context.AliOssContext;
import com.hw.backapi.context.AwsOssContext;
import com.hw.backapi.service.IOssService;
import com.hw.common.contants.UrlContants;
import org.junit.Test;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@RestController
public class OssController {

    @Autowired
    IOssService ossServiceImpl;

    /**
     * @descrition 导出患者oss文件
     * @author lipeng
     * @date 2023/3/8
     * @param ossId
     * @param response
     * @param exportType 1csv 2video
     * @return void
     */
    @GetMapping(UrlContants.EXPORT_OSS)
    public void exportOss(
            @RequestParam("ossId") Long ossId, @RequestParam(name = "exportType",required = false) Long exportType, HttpServletResponse response
    ){
        ossServiceImpl.exportOss(ossId,exportType,response);
    }





    //下面的都是测试用的=======================

    @Autowired
    AliOssContext aliOssContext;

    @Autowired
    AwsOssContext awsOssContext;

    @GetMapping("oss")
    public void test() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("1.png");

        aliOssContext.uploadOssFile("1.png",fileInputStream);
    }

    @GetMapping("downoss")
    public void downoss(@RequestParam("fileName") String name,HttpServletResponse response) throws FileNotFoundException {
        aliOssContext.downloadFile(name,"a.csv",response);
    }

    @GetMapping("generateUrl")
    public String generateUrl(
            @RequestParam("fileName") String name
    ){

        return aliOssContext.generateUrl(name);
    }

    @GetMapping("listfile")
    public void listFile(){
        aliOssContext.listFile();
    }


    //aws操作
    @GetMapping("awsoss")
    public void awsoss() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("1.png");

        awsOssContext.uploadOssFile("1.png",fileInputStream);
    }

    @GetMapping("awsdownoss")
    public void downoss1(@RequestParam("fileName") String name,HttpServletResponse response) throws FileNotFoundException {
        awsOssContext.downloadFile(name,"22.png",response);
    }


    @Test
    public void test1(){
        String password = "1234567";

        // 加密
        String encodedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
        System.out.println(encodedPassword);

        // 使用正确密码验证密码是否正确
        boolean flag = BCrypt.checkpw(password, encodedPassword);
        System.out.println(flag);

        // 使用错误密码验证密码是否正确
        flag = BCrypt.checkpw("111222", encodedPassword);
        System.out.println(flag);

        System.out.println("-------------------------------------------");

    }
}
